package com.zhengsr.latte.ui.recycler.adapter.base;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zhengsr.latte.ui.recycler.adapter.callback.RBaseListenerAdapter;


/**
 * Created by zhengshaorui on 2018/5/26.
 */

public abstract class RBaseAdapter extends RecyclerView.Adapter<RBaseViewholder> {


    private RBaseListenerAdapter mRBaseLIstenerAdapter;
    public void setOnItemClickListener(RBaseListenerAdapter listener){
        mRBaseLIstenerAdapter = listener;
    }
    public RBaseListenerAdapter getListener(){
        return mRBaseLIstenerAdapter;
    }

    /**
     * 绑定item点击事件
     * @param viewholder
     */
    protected void bindClickListener(final RBaseViewholder viewholder, final RBaseAdapter adapter){
        if (viewholder != null){

            //判定子控件
            viewholder.setAdapter(adapter);

            //判定 item 事件
            View view = viewholder.getConserView();
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mRBaseLIstenerAdapter != null) {
                        mRBaseLIstenerAdapter.onItemClick( v,
                                viewholder.getLayoutPosition());
                    }
                }
            });
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (mRBaseLIstenerAdapter != null) {
                        mRBaseLIstenerAdapter.onItemLongClick( v,
                                viewholder.getLayoutPosition());
                        return true;
                    }
                    return false;
                }
            });
        }
    }

}
